home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / CODECS.ZIP / codecs / english / compress.txt < prev    next >
Encoding:
Text File  |  1995-10-13  |  48.5 KB  |  1,091 lines

  1. +======================================================+
  2. |  Introduction to the losslessy compression schemes   |
  3. |   Description of the source files and the methods    |
  4. +------------------------------------------------------+
  5. | From David Bourgin (E-mail: dbourgin@ufrima.imag.fr) |
  6. | Date: 12/10/95                          VERSION: 1.5 |
  7. +======================================================+
  8.  
  9.               ------ BE CARE ------
  10. This file must be given in a package containing the following files into two
  11. directories:
  12. * French files in codecs.dir/francais directory:
  13. lisezmoi,compress.txt,codrle1.c,codrle2.c,codrle3.c,codrle4.c,codhuff.c,codlzw.c,
  14. dcodrle1.c,dcodrle2.c,dcodrle3.c,dcodrle4.c,dcodhuff.c,dcodlzw.c
  15. * English files in codecs.dir/english directory:
  16. readme,compress.txt,codrle1.c,codrle2.c,codrle3.c,codrle4.c,codhuff.c,codlzw.c,
  17. dcodrle1.c,dcodrle2.c,dcodrle3.c,dcodrle4.c,dcodhuff.c,dcodlzw.c
  18.  
  19. Please read the file 'readme' to get more infos about copyrights and the
  20. contents of the files.
  21.               ---------------------
  22.  
  23. There are several means to compress data. Here, we are only going to deal with
  24. the losslessy schemes. These schemes are also called non-destructive because
  25. you always recover the initial data you had, and this, as soon as you need them.
  26. With losslessy schemes, you won't never lose any informations (except perhaps
  27. when you store or transmit your data but this is another problem...).
  28.  
  29. In this introduction, we are going to see:
  30. - The RLE scheme (with different possible algorithms)
  31. - The Huffman schemes (dynamical scheme)
  32. - And the LZW scheme
  33.  
  34. For the novice, a compresser is a program able to read several data (e.g. bytes)
  35. in input and to write several data in output. The data you obtain from the
  36. output (also called compressed data) will - of course - take less space than
  37. the the input data. This is true in most of cases, if the compresser works
  38. and if the type of the data is correct to be compressed with the given scheme.
  39. The codec (coder-decoder) enables you to save space on your hard disk and/or
  40. to save the communication costs because you always store/transmit the compressed
  41. data. You'll use the decompresser as soon as you need to recover your initial
  42. useful data. Note that the compressed data are useless if you have not
  43. the decoder...
  44.  
  45. You are doubtless asking "How can I reduce the data size without losing some
  46. informations?". It's easy to answer to this question. I'll only take an example.
  47. I'm sure you have heard about the morse. This system established in the 19th
  48. century use a scheme very close to the huffman one. In the morse you encode
  49. the letters to transmit with two kinds of signs. If you encode these two sign
  50. possibilities in one bit, the symbol 'e' is transmitted in a single bit and
  51. the symbols 'y' and 'z' need four bits. Look at the symbols in the text you are
  52. reading, you'll fast understand the compression ratio...
  53.  
  54. From there I explain into two parts:
  55. - How to change the source codes
  56. - What are RLE1, RLE2, RLE3, Huffman, and LZW encoding/decoding
  57.  
  58. ********** FIRST PART: Source modifications you can do **********
  59.  
  60. Important: The source codes associated to the algorithms I present are
  61. completely adaptative on what you need to compress. They all use basical
  62. macros on the top of the file. Usually the macros to change are:
  63.  
  64. - beginning_of_data
  65. - end_of_data
  66. - read_byte
  67. - read_block
  68. - write_byte
  69. - write_block
  70.  
  71. These allow the programmer to modify only a little part of the header
  72. of the source codes in order to compress as well memory as files.
  73.  
  74. beginning_of_data(): Macro used to set the program so that the next read_byte()
  75. call will read the first byte to compress.
  76. end_of_data(): Returns a boolean to know whether there is no more bytes to read
  77. from the input stream. Return 0 if there is no more byte to compress, another
  78. non-zero value otherwise.
  79. read_byte(): Returns a byte read from the input stream if available.
  80. write_byte(x): Writes the byte 'x' to the output stream.
  81. read_block(...) and write_block(...): Same use as read_byte and write_byte(x)
  82. but these macros work on blocks of bytes and not only on a single byte.
  83.  
  84. If you want to compress *from* the memory, before entering in a xxxcoding
  85. procedure ('xxx' is the actual extension to replace with a given codec), you
  86. have to add a pointer set up to the beginning of the zone to compress. Note
  87. that the following pointer 'source_memory_base' is not to add, it is just given
  88. here to specify a name to the address of the memory zone you are going to
  89. encode or decode. That is the same about source_memory_end which can be either
  90. a pointer to create or an existing pointer.
  91.  
  92. unsigned char *source_memory_base, /* Base of the source memory */
  93.           *source_memory_end,  /* Last address to read.
  94. source_memory_end=source_memory_base+source_zone_length-1 */
  95.           *source_ptr;         /* Used in the xxxcoding procedure */
  96. void pre_start()
  97. { source_ptr=source_memory_base;
  98.   xxxcoding();
  99. }
  100.  
  101. end_of_data() and read_byte() are also to modify to compress *from* memory:
  102.  
  103. #define end_of_data()  (source_ptr>source_memory_end)
  104. #define read_byte()  (*(source_ptr++))
  105.  
  106. If you want to compress *to* memory, before entering in a xxxcoding procedure
  107. ('xxx' is the actual extension to replace with a given codec), you have to add
  108. a pointer. Note that the pointer 'dest_memory_base' is not to add, it is just
  109. given there to specify the address of the destination memory zone you are
  110. going to encode or decode.
  111.  
  112. unsigned char *dest_memory_base, /* Base of the destination memory */
  113.           *dest_ptr;         /* Used in the xxxcoding procedure */
  114. void pre_start()
  115. { dest_ptr=dest_memory_base;
  116.   xxxcoding();
  117. }
  118.  
  119. Of course, you can combine both from and to memory in the pre_start() procedure.
  120. The files dest_file and source_file handled in the main() function are
  121. to remove...
  122.  
  123. void pre_start()
  124. { source_ptr=source_memory_base;
  125.   dest_ptr=dest_memory_base;
  126.   xxxcoding();
  127. }
  128.  
  129. In fact, to write to memory, the problem is in the write_byte(x) procedure.
  130. This problem exists because your destination zone can either be a static
  131. zone or a dynamically allocated zone. In the two cases, you have to check
  132. if there is no overflow, especially if the coder is not efficient and must
  133. produce more bytes than you reserved in memory.
  134.  
  135. In the first case, with a *static* zone, write_byte(x) macro should look like
  136. that:
  137.  
  138. unsigned long int dest_zone_length,
  139.           current_size;
  140.  
  141. #define write_byte(x)  { if (current_size==dest_zone_length) \
  142.                 exit(1); \
  143.              dest_ptr[current_size++]=(unsigned char)(x); \
  144.                }
  145.  
  146. In the static version, the pre_start() procedure is to modify as following:
  147.  
  148. void pre_start()
  149. { source_ptr=source_memory_base;
  150.   dest_ptr=dest_memory_base;
  151.   dest_zone_length=...; /* Set up to the actual destination zone length */
  152.   current_size=0; /* Number of written bytes */
  153.   xxxcoding();
  154. }
  155. Otherwise, dest_ptr is a zone created by the malloc instruction and you can try
  156. to resize the allocated zone with the realloc instruction. Note that I increment
  157. the zone one kilo-bytes by one kylo-bytes. You have to add two other variables:
  158.  
  159. unsigned long int dest_zone_length,
  160.           current_size;
  161.  
  162. #define write_byte(x)  { if (current_size==dest_zone_length) \
  163.                 { dest_zone_length += 1024; \
  164.                   if ((dest_ptr=(unsigned char *)realloc(dest_ptr,dest_zone_length*sizeof(unsigned char)))==NULL) \
  165.                  exit(1); /* You can't compress in memory \
  166.                            => I exit but *you* can make a routine to swap on disk */ \
  167.                 } \
  168.              dest_ptr[current_size++]=(unsigned char)(x); \
  169.                }
  170.  
  171. With the dynamically allocated version, change the pre_start() routine as following:
  172.  
  173. void pre_start()
  174. { source_ptr=source_memory_base;
  175.   dest_ptr=dest_memory_base;
  176.   dest_zone_length=1024;
  177.   if ((dest_ptr=(unsigned char *)malloc(dest_zone_length*sizeof(unsigned char)))==NULL)
  178.      exit(1); /* You need at least 1 kb in the dynamical memory ! */
  179.   current_size=0; /* Number of written bytes */
  180.   xxxcoding();
  181.   /* Handle the bytes in dest_ptr but don't forget to free these bytes with:
  182.      free(dest_ptr);
  183.   */
  184. }
  185.  
  186. The previously given macros work as:
  187.  
  188. void demo()       /* The file opening, closing and variables
  189.              must be set up by the calling procedure */
  190. { unsigned char byte;
  191.           /* And not 'char byte' (!) */
  192.   while (!end_of_data())
  193.     { byte=read_byte();
  194.       printf("Byte read=%c\n",byte);
  195.     }
  196. }
  197.  
  198. You must not change the rest of the program unless you're really sure and
  199. really need to do it!
  200.  
  201. ********** SECOND PART: Encoding/Decoding explainations **********
  202.  
  203. +==========================================================+
  204. |                     The RLE encoding                     |
  205. +==========================================================+
  206.  
  207. RLE is an acronym that stands for Run Length Encoding. You may encounter it
  208. as an other acronym: RLC, Run Length Coding.
  209.  
  210. The idea in this scheme is to recode your data with regard to the repetition
  211. frames. A frame is one or more bytes that occurr one or several times.
  212.  
  213. There are several means to encode occurrences. So, you'll have several codecs.
  214. For example, you may have a sequence such as:
  215. 0,0,0,0,0,0,255,255,255,2,3,4,2,3,4,5,8,11
  216.  
  217. Some codecs will only deal with the repetitions of '0' and '255' but some other
  218. will deal with the repetitions of '0', '255', and '2,3,4'.
  219.  
  220. You have to keep in your mind something important based on this example. A codec
  221. won't work on all the data you will try to compress. So, in case of non
  222. existence of sequence repetitions, the codecs based on RLE schemes must not
  223. display a message to say: "Bye bye". Actually, they will try to encode these
  224. non repeted data with a value that says "Sorry, I only make a copy of the inital
  225. input". Of course, a copy of the input data with an header in front of this copy
  226. will make a biggest output data but if you consider the whole data to compress,
  227. the encoding of repeated frames will take less space than the encoding
  228. of non-repeated frames.
  229.  
  230. All of the algorithms with the name of RLE have the following look with three
  231. or four values:
  232. - Value saying if there's a repetition
  233. - Value saying how many repetitions (or non repetition)
  234. - Value of the length of the frame (useless if you just encode frame
  235. with one byte as maximum length)
  236. - Value of the frame to repeat (or not)
  237.  
  238. I gave four algorithms to explain what I say.
  239.  
  240. *** First RLE scheme ***
  241.  
  242. The first scheme is the simpliest I know, and looks like the one used in MAC
  243. system (MacPackBit) and some image file formats such as Targa, PCX, TIFF, ...
  244.  
  245. Here, all compressed blocks begin with a byte, named header, which description
  246. is:
  247.  
  248. Bits   7 6 5 4 3 2 1 0
  249. Header X X X X X X X X
  250.  
  251. Bits 7: Compression status (1=Compression applied)
  252.      0 to 6: Number of bytes to handle
  253.  
  254. So, if the bit 7 is set up to 0, the 0 to 6 bits give the number of bytes
  255. that follow (minus 1, to gain more over compress) and that were not compressed
  256. (native bytes). If the bit 7 is set up to 1, the same 0 to 6 bits give
  257. the number of repetition (minus 2) of the following byte.
  258.  
  259. As you see, this method only handle frame with one byte.
  260.  
  261. Additional note: You have 'minus 1' for non-repeated frames because you must
  262. have at least one byte to compress and 'minus 2' for repeated frames because the
  263. repetition must be 2, at least.
  264.  
  265. Compression scheme:
  266.  
  267.           First byte=Next
  268.             /\
  269.            /  \
  270. Count the byte         Count the occurrence of NON identical
  271. occurrences            bytes (maximum 128 times)
  272. (maximum 129 times)    and store them in an array
  273.     |                        |
  274.     |                        |
  275.   1 bit '1'                 1 bit '0'
  276. + 7 bits giving           + 7 bits giving
  277.   the number (-2)           the number (-1)
  278.   of repetitions            of non repetition
  279. + repeated byte           + n non repeated bytes
  280.     |                        |
  281.  1xxxxxxx,yyyyyyyy        0xxxxxxx,n bytes
  282. [-----------------]      [----------------]
  283.  
  284. Example:
  285.  
  286. Sequence of bytes to encode | Coded values | Differences with compression
  287.                 |              |         (unit: byte)
  288. -------------------------------------------------------------------------
  289.        255,15,              |  1,255,15,   |            -1
  290.        255,255,             |    128,255,  |             0
  291.     15,15,              |    128,15,   |             0
  292.      255,255,255,           |   129,255,   |            +1
  293.        15,15,15,            |    129,15,   |            +1
  294.    255,255,255,255,         |   130,255,   |            +2
  295.      15,15,15,15            |    130,15    |            +2
  296.  
  297. See codecs source codes: codrle1.c and dcodrle1.c
  298.  
  299. *** Second RLE scheme ***
  300.  
  301. In the second scheme of RLE compression you look for the less frequent byte
  302. in the source to compress and use it as an header for all compressed block.
  303.  
  304. In the best cases, the occurrence of this byte is zero in the data to compress.
  305.  
  306. Two possible schemes, firstly with handling frames with only one byte,
  307. secondly with handling frames with one byte *and* more. The first case is
  308. the subject of this current compression scheme, the second is the subject
  309. of next compression scheme.
  310.  
  311. For the frame of one byte, header byte is written in front of all repetition
  312. with at least 4 bytes. It is then followed by the repetition number minus 1 and
  313. the repeated byte.
  314. Header byte, Occurrence number-1, repeated byte
  315.  
  316. If a byte don't repeat more than tree times, the three bytes are written without
  317. changes in the destination stream (no header nor length, nor repetition in front
  318. or after theses bytes).
  319.  
  320. An exception: If the header byte appears in the source one, two, three and up
  321. times, it'll be respectively encoded as following:
  322. - Header byte, 0
  323. - Header byte, 1
  324. - Header byte, 2
  325. - Header byte, Occurrence number-1, Header byte
  326.  
  327. Example, let's take the previous example. A non frequent byte is zero-ASCII
  328. because it never appears.
  329.  
  330. Sequence of bytes to encode | Coded values | Differences with compression
  331.                 |              |         (unit: byte)
  332. -------------------------------------------------------------------------
  333.        255,15,              |    255,15,   |             0
  334.        255,255,             |   255,255,   |             0
  335.     15,15,              |     15,15,   |             0
  336.      255,255,255,           | 255,255,255, |             0
  337.        15,15,15,            |   15,15,15,  |             0
  338.    255,255,255,255,         |   0,3,255,   |            -1
  339.      15,15,15,15            |    0,3,15    |            -1
  340.  
  341. If the header would appear, we would see:
  342.  
  343. Sequence of bytes to encode | Coded values | Differences with compression
  344.                 |              |         (unit: byte)
  345. -------------------------------------------------------------------------
  346.       0,                |      0,0,    |            +1
  347.      255,               |      255,    |             0
  348.      0,0,               |      0,1,    |             0
  349.      15,                |      15,     |             0
  350.     0,0,0,              |      0,2,    |            -1
  351.      255,               |      255,    |             0
  352.        0,0,0,0              |     0,3,0    |            -1
  353.  
  354. See codecs source codes: codrle2.c and dcodrle2.c
  355.  
  356. *** Third RLE scheme ***
  357.  
  358. It's the same idea as the second scheme but we can encode frames with
  359. more than one byte. So we have three cases:
  360.  
  361. - If it was the header byte, whatever is its occurrence, you encode it with:
  362. Header byte,0,number of occurrence-1
  363. - For frames which (repetition-1)*length>3, encode it as:
  364. Header byte, Number of frame repetition-1, frame length-1,bytes of frame
  365. - If no previous cases were detected, you write them as originally (no header,
  366. nor length, nor repetition in front or after theses bytes).
  367.  
  368. Example based on the previous examples:
  369.  
  370. Sequence of bytes to encode |   Coded values   | Differences with compression
  371.                 |                  |         (unit: byte)
  372. -----------------------------------------------------------------------------
  373.        255,15,          |      255,15,     |             0
  374.        255,255,         |     255,255,     |             0
  375.         15,15,          |       15,15,     |             0
  376.      255,255,255,       |   255,255,255,   |             0
  377.        15,15,15,        |     15,15,15,    |             0
  378.        255,255,255,255,     | 255,255,255,255, |             0
  379.      15,15,15,15,       |   15,15,15,15,   |             0
  380.       16,17,18,16,17,18,    |16,17,18,16,17,18,|             0
  381.      255,255,255,255,255,   |    0,4,0,255,    |            -1
  382.        15,15,15,15,15,      |     0,4,0,15,    |            -1
  383.  16,17,18,16,17,18,16,17,18,|  0,2,2,16,17,18, |            -3
  384.   26,27,28,29,26,27,28,29   |0,1,3,26,27,28,29 |            -1
  385.  
  386. If the header (value 0) would be met, we would see:
  387.  
  388. Sequence of bytes to encode | Coded values  | Differences with compression
  389.                 |               |         (unit: byte)
  390. --------------------------------------------------------------------------
  391.       0,                |     0,0,0,    |            +2
  392.      255,               |      255,     |             0
  393.      0,0,               |     0,0,1,    |            +1
  394.       15,               |       15,     |             0
  395.     0,0,0,              |     0,0,2,    |             0
  396.      255,               |      255,     |             0
  397.        0,0,0,0              |     0,0,3     |            -1
  398.  
  399. See codecs source codes: codrle3.c and dcodrle3.c
  400.  
  401. *** Fourth RLE scheme ***
  402.  
  403. This last RLE algorithm better handles repetitions of any kind (one byte
  404. and more) and non repetitions, including few non repetitions, and does not
  405. read the source by twice as RLE type 3.
  406.  
  407. Compression scheme is:
  408.  
  409.           First byte=Next byte?
  410.                /\
  411.               Yes /  \ No
  412.              /    \
  413.          1 bit '0'     1 bit '1'
  414.                /        \
  415.               /          \
  416.        Count the                    Motif of several
  417.        occurrences                  repeated  byte?
  418.        of 1 repeated                ( 65 bytes repeated
  419.        byte (maximum                257 times maxi)
  420.        16449 times)                           /\
  421.         /\                               /  \
  422.        /  \                             /    \
  423.       /    \                           /      \
  424.      /      \                         /        \
  425.   1 bit '0'       1 bit '1'        1 bit '0'          1 bit '1'
  426. + 6 bits        + 14 bits        + 6 bits of              |
  427. giving the      giving the       the length      Number of non repetition
  428. length (-2)     length (-66)     of the motif         (maximum 8224)
  429. of the          of the           + 8 bits of               /\
  430. repeated byte   repeated byte    the number (-2)     < 33 /  \ > 32
  431. + repeated byte + repeated byte  of repetition           /    \
  432.     |                |           + bytes of the   1 bit '0'       1 bit '1'
  433.     |                |           motif          + 5 bits of     + 13 bits
  434.     |                |               |          the numer (-1)  of the
  435.     |                |               |          of non          number (-33)
  436.     |                |               |          repetition      of repetition
  437.     |                |               |          + non           + non
  438.     |                |               |          repeated        repeated
  439.     |                |               |          bytes           bytes
  440.     |                |               |             |               |
  441.     |                |               |             |  111xxxxx,xxxxxxxx,n bytes
  442.     |                |               |             | [-------------------------]
  443.     |                |               |             |
  444.     |                |               |      110xxxxx,n bytes
  445.     |                |               |     [----------------]
  446.     |                |               |
  447.     |                |  10xxxxxx,yyyyyyyy,n bytes
  448.     |                | [-------------------------]
  449.     |                |
  450.     |   01xxxxxx,xxxxxxxx,1 byte
  451.     |  [------------------------]
  452.     |
  453.  00xxxxxx,1 byte
  454. [---------------]
  455.  
  456. Example, same as previously:
  457.  
  458. Sequence of bytes to encode |         Coded values          | Differences with compression
  459.                 |                               |         (unit: byte)
  460. ------------------------------------------------------------------------------------------
  461.     255,15,255,255,15,15    |11000101b,255,15,255,255,15,15 |             +1
  462.      255,255,255            |        00000001b,255,         |             -1
  463.        15,15,15             |         00000001b,15,         |             -1
  464.    255,255,255,255          |         00000010b,255,        |             -2
  465.      15,15,15,15            |          00000010b,15,        |             -2
  466.   16,17,18,16,17,18         |     10000001b,0,16,17,18,     |             -1
  467.  255,255,255,255,255        |        00000011b,255,         |             -3
  468.    15,15,15,15,15           |         00000011b,15,         |             -3
  469.  16,17,18,16,17,18,16,17,18 |      10000001b,16,17,18,      |             -4
  470.   26,27,28,29,26,27,28,29   |     10000010b,26,27,28,29     |             -2
  471.  
  472.  
  473. +==========================================================+
  474. |                   The Huffman encoding                   |
  475. +==========================================================+
  476.  
  477. This method comes from the searcher who established the algorithm in 1952.
  478. This method allows both a dynamic and static statistic schemes. A statistic
  479. scheme works on the data occurrences. It is not as with RLE where you had
  480. a consideration of the current occurrence of a frame but rather a consideration
  481. of the global occurrences of each data in the input stream. In this last case,
  482. frames can be any kinds of sequences you want. On the other hand, Huffman
  483. static encoding appears in some compressers such as ARJ on PCs. This enforces
  484. the encoder to consider every statistic as the same for all the data you have.
  485. Of course, the results are not as good as if it were a dynamic encoding.
  486. The static encoding is faster than the dynamic encoding but the dynamic encoding
  487. will be adapted to the statistic of the bytes of the input stream and will
  488. of course become more efficient by producing shortest output.
  489.  
  490. The main idea in Huffman encoding is to re-code every byte with regard to its
  491. occurrence. The more frequent bytes in the data to compress will be encoded with
  492. less than 8 bits and the others could need 8 bits see even more to be encoded.
  493. You immediately see that the codes associated to the different bytes won't have
  494. identical size. The Huffman method will actually require that the binary codes
  495. have not a fixed size. We speak then about variable length codes.
  496.  
  497. The dynamical Huffman scheme needs the binary trees for the encoding. This
  498. enables you to obtain the best codes, adapted to the source data.
  499. The demonstration won't be given there. To help the neophyt, I will just explain
  500. what is a binary tree.
  501.  
  502. A binary tree is special fashion to represent the data. A binary tree is
  503. a structure with an associated value with two pointers. The term of binary has
  504. been given because of the presence of two pointers. Because of some conventions,
  505. one of the pointer is called left pointer and the second pointer is called right
  506. pointer. Here is a visual representation of a binary tree.
  507.  
  508.      Value
  509.       / \
  510.      /   \
  511.  Value    Value
  512.   / \      / \
  513. ... ...  ... ...
  514.  
  515. One problem with a binary encoding is a prefix problem. A prefix is the first
  516. part of the representation of a value, e.g. "h" and "he" are prefixes of "hello"
  517. but not "el". To understand the problem, let's code the letters "A", "B", "C",
  518. "D", and "E" respectively as 00b, 01b, 10b, 11b, and 100b. When you read
  519. the binary sequence 00100100b, you are unable to say if this comes from "ACBA"
  520. or "AEE". To avoid such situations, the codes must have a prefix property.
  521. And the letter "E" mustn't begin with the sequence of an other code. With "A",
  522. "B", "C", "D", and "E" respectively affected with 1b, 01b, 001b, 0001b, and
  523. 0000b, the sequence 1001011b will only be decoded as "ACBA".
  524.  
  525.  1      0
  526. <-  /\  ->
  527.    /  \
  528.  "A"  /\
  529.     "B" \
  530.     /\
  531.       "C" \
  532.       /\
  533.        "D"  "E"
  534.  
  535. As you see, with this tree, an encoding will have the prefix property
  536. if the bytes are at the end of each "branch" and you have no byte at the "node".
  537. You also see that if you try to reach a character by the right pointer you add
  538. a bit set to 0 and by the left pointer, you add a bit set to 1 to the current
  539. code. The previous *bad* encoding provide the following bad tree:
  540.  
  541.        /\
  542.       /  \
  543.      /    \
  544.     /\    /\
  545.    /  \ "B" "A"
  546.   /    \
  547. "D"  "C"\
  548.       /  \
  549.      "E"
  550.  
  551. You see here that the coder shouldn't put the "C" at a node...
  552.  
  553. As you see, the largest binary code are those with the longest distance
  554. from the top of the tree. Finally, the more frequent bytes will be the highest
  555. in the tree in order you have the shortest encoding and the less frequent bytes
  556. will be the lowest in the tree.
  557.  
  558. From an algorithmic point of view, you make a list of each byte you encountered
  559. in the stream to compress. This list will always be sorted. The zero-occurrence
  560. bytes are removed from this list. You take the two bytes with the smallest
  561. occurrences in the list. Whenever two bytes have the same "weight", you take two
  562. of them regardless to their ASCII value. You join them in a node. This node will
  563. have a fictive byte value (256 will be a good one!) and its weight will be
  564. the sum of the two joined bytes. You replace then the two joined bytes with
  565. the fictive byte. And you continue so until you have one byte (fictive or not)
  566. in the list. Of course, this process will produce the shortest codes if the list
  567. remains sorted. I will not explain with arcana hard maths why the result
  568. is a set of the shortest bytes...
  569.  
  570. Important: I use as convention that the right sub-trees have a weight greater
  571. or equal to the weight of the left sub-trees.
  572.  
  573. Example: Let's take a file to compress where we notice the following
  574. occurrences:
  575.  
  576. Listed bytes | Frequences (Weight)
  577. ----------------------------------
  578.       0      |        338
  579.      255     |        300
  580.       31     |        280
  581.       77     |         24
  582.      115     |         21
  583.       83     |         20
  584.      222     |         5
  585.  
  586. We will begin by joining the bytes 83 and 222. This will produce a fictive node
  587. 1 with a weight of 20+5=25.
  588.  
  589. (Fictive 1,25)
  590.       /\
  591.      /  \
  592. (222,5) (83,20)
  593.  
  594. Listed bytes | Frequences (Weight)
  595. ----------------------------------
  596.       0      |        338
  597.      255     |        300
  598.       31     |        280
  599.   Fictive 1  |         25
  600.       77     |         24
  601.      115     |         21
  602.  
  603. Note that the list is sorted... The smallest values in the frequences are 21 and
  604. 24. That is why we will take the bytes 77 and 115 to build the fictive node 2.
  605.  
  606. (Fictive 2,45)
  607.       /\
  608.      /  \
  609. (115,21) (77,25)
  610.  
  611. Listed bytes | Frequences (Weight)
  612. ----------------------------------
  613.       0      |        338
  614.      255     |        300
  615.       31     |        280
  616.   Fictive 2  |         45
  617.   Fictive 1  |         25
  618.  
  619. The nodes with smallest weights are the fictive 1 and 2 nodes. These are joined
  620. to build the fictive node 3 whose weight is 40+25=70.
  621.  
  622.     (Fictive 3,70)
  623.          /   \
  624.        /       \
  625.      /           \
  626.        /\            / \
  627.      /   \          /    \
  628. (222,5)  (83,20) (115,21) (77,25)
  629.  
  630. Listed bytes | Frequences (Weight)
  631. ----------------------------------
  632.       0      |        338
  633.      255     |        300
  634.       31     |        280
  635.   Fictive 3  |         70
  636.  
  637. The fictive node 3 is linked to the byte 31. Total weight: 280+70=350.
  638.  
  639.          (Fictive 4,350)
  640.            /   \
  641.          /       \
  642.            /           \
  643.          /  \       (31,280)
  644.        /      \
  645.      /          \
  646.        /\            / \
  647.      /   \          /    \
  648. (222,5)  (83,20) (115,21) (77,25)
  649.  
  650. Listed bytes | Frequences (Weight)
  651. ----------------------------------
  652.   Fictive 4  |        350
  653.       0      |        338
  654.      255     |        300
  655.  
  656. As you see, being that we sort the list, the fictive node 4 has become the first
  657. of the list. We join the bytes 0 and 255 in a same fictive node, the number 5
  658. whose weight is 338+300=638.
  659.  
  660. (Fictive 5,638)
  661.     /\
  662.        /  \
  663. (255,300) (0,338)
  664.  
  665. Listed bytes | Frequences (Weight)
  666. ----------------------------------
  667.   Fictive 5  |        638
  668.   Fictive 4  |        350
  669.  
  670. The fictive nodes 4 and 5 are finally joined. Final weight: 638+350=998 bytes.
  671. It is actually the total byte number in the initial file: 338+300+24+21+20+5.
  672.  
  673.              (Tree,998)
  674.                1     /  \     0
  675.               <-   /      \   ->
  676.              /          \
  677.                /              \
  678.              /                  \
  679.            /   \                / \
  680.          /       \             /    \
  681.            /           \          /       \
  682.          /  \       (31,280)  (255,300) (0,338)
  683.        /      \
  684.      /          \
  685.        /\            / \
  686.      /   \          /    \
  687. (222,5)  (83,20) (115,21) (77,25)
  688.  
  689. Bytes | Huffman codes | Frequences | Binary length*Frequence
  690. ------------------------------------------------------------
  691.   0   |       00b     |     338    |           676
  692.  255  |       01b     |     300    |           600
  693.   31  |       10b     |     280    |           560
  694.   77  |      1101b    |      24    |            96
  695.  115  |      1100b    |      21    |            84
  696.   83  |      1110b    |      20    |            80
  697.  222  |      1111b    |      5     |            20
  698.  
  699. Results: Original file size: (338+300+280+24+21+20+5)*8=7904 bits (=998 bytes)
  700. versus 676+600+560+96+84+80+20=2116 bits, i.e. 2116/8=265 bytes.
  701.  
  702. Now you know how to code an input stream. The last problem is to decode all this
  703. stuff. Actually, when you meet a binary sequence you can't say whether it comes
  704. from such byte list or such other one. Furthermore, if you change the occurrence
  705. of one or two bytes, you won't obtain the same resulting binary tree. Try for
  706. example to encode the previous list but with the following occurrences:
  707.  
  708. Listed bytes | Frequences (Weight)
  709. ----------------------------------
  710.      255     |        418
  711.       0      |        300
  712.       31     |        100
  713.       77     |         24
  714.      115     |         21
  715.       83     |         20
  716.      222     |         5
  717.  
  718. As you can observe it, the resulting binary tree is quite different, we had yet
  719. the same initial bytes. To not be in such a situation we will put an header
  720. in front of all data. I can't comment longly this header but I can say
  721. I minimize it as much as I could. The header is divided into two parts.
  722. The first part of this header looks closely to a boolean table (coded more or
  723. less in binary to save space) and the second part provide to the decoder
  724. the binary code associated to each byte encountered in the original input
  725. stream.
  726.  
  727. Here is a summary of the header:
  728.  
  729. First part
  730. ----------
  731.             First bit
  732.               /  \
  733.               1 /      \ 0
  734.               /          \
  735.   256 bits set to 0 or 1        5 bits for the number n (minus 1)
  736.   depending whether the         of bytes encountered
  737.   corresponding byte was        in the file to compres
  738.   in the file to compress                   |
  739.   (=> n bits set to 1,                     \ /
  740.    n>32)                        n values of 8-bits (n<=32)
  741.              \           /
  742.                \       /
  743.              \   /
  744. Second part                |
  745. -----------                |
  746.                |
  747.         +------------->|
  748. (n+1) times |              |
  749. (n bytes of |          First bit?
  750. the values  |            /   \
  751. encountered |         1 /      \ 0
  752. in the      |          /        \ 
  753. source file |   8 bits of      5 bits of the 
  754. + the code  | the length       length (-1)
  755. of a        | (-1) of the      of the following
  756. fictive     | following        binary
  757. byte        | binary code      code
  758. to stop the | (length>32)      (length<=32)
  759. decoding.   |          \       /
  760. The fictive |           \     /
  761. is set to   |            \   /
  762. 256 in the  |              |
  763. Huffman     |         binary code
  764. -tree of    |              |
  765. encoding)   +--------------|
  766.                |
  767.         Binary encoding of the source file
  768.                |
  769.           Code of end of encoding
  770.                |
  771.  
  772.  
  773. With my codecs I can handle binary sequences with a length of 256 bits.
  774. This correspond to encode all the input stream from one byte to infinite length.
  775. In fact if a byte had a range from 0 to 257 instead of 0 to 255, I would have a
  776. bug with my codecs with an input stream of at least 370,959,230,771,131,880,927,
  777. 453,318,055,001,997,489,772,178,180,790,105 bytes !!!
  778.  
  779. Where come this explosive number? In fact, to have a severe bug, I must have
  780. a completely unbalanced tree:
  781.  
  782.    Tree
  783.     /\
  784.       \
  785.       /\
  786.     \
  787.     /\
  788.       \
  789.       ...
  790.        /\
  791.          \
  792.          /\
  793.  
  794. Let's take the following example:
  795.  
  796. Listed bytes | Frequences (Weight)
  797. ----------------------------------
  798.       32     |         5
  799.       101    |         3
  800.       97     |         2
  801.       100    |         1
  802.       115    |         1
  803.  
  804. This produces the following unbalanced tree:
  805.  
  806.     Tree
  807.      /\
  808. (32,5) \
  809.        /\
  810.  (101,3) \
  811.      /\
  812.    (97,2)  \
  813.        /\
  814.     (115,1)  (100,1)
  815.  
  816. Let's speak about a mathematical series: The Fibonacci series. It is defined as
  817. following:
  818.  
  819. { Fib(0)=0
  820. { Fib(1)=1
  821. { Fib(n)=Fib(n-2)+Fib(n-1)
  822.  
  823. Fib(0)=0, Fib(1)=1, Fib(2)=1, Fib(3)=2, Fib(4)=3, Fib(5)=5, Fib(6)=8, Fib(7)=13,
  824. etc.
  825.  
  826. But 1, 1, 2, 3, 5, 8 are the occurrences of our list! We can actually
  827. demonstrate that to have an unbalanced tree, we have to take a list with
  828. an occurrence based on the Fibonacci series (these values are minimal).
  829. If the data to compress have m different bytes, when the tree is unbalanced,
  830. the longest code need m-1 bits. In our little previous example where m=5,
  831. the longest codes are associated to the bytes 100 and 115, respectively coded
  832. 0001b and 0000b. We can also say that to have an unbalanced tree we must have
  833. at least 5+3+2+1+1=12=Fib(7)-1. To conclude about all that, with a coder that
  834. uses m-1 bits, you must never have an input stream size over than Fib(m+2)-1,
  835. otherwise, there could be a bug in the output stream. Of course, with my codecs
  836. there will never be a bug because I can deal with binary code sizes of 1 to 256
  837. bits. Some encoder could use that with m=31, Fib(31+2)-1=3,524,577 and m=32,
  838. Fib(32+2)-1=5,702,886. And an encoder that uses unisgned integer of 32 bits
  839. shouldn't have a bug until about 4 Gb...
  840.  
  841. +==========================================================+
  842. |                     The LZW encoding                     |
  843. +==========================================================+
  844.  
  845. The LZW scheme is due to three searchers, i.e. Abraham Lempel and Jacob Ziv
  846. worked on it in 1977, and Terry Welch achieved this scheme in 1984.
  847.  
  848. LZW is patented in USA. This patent, number 4,558,302, is covered by Unisys
  849. Corporation and CompuServe. IBM seems to have discovered the same, and patented
  850. it. (Who's right???)
  851. You may get a limited licence by writting to:
  852. Welch Licencing Department
  853. Office of the General Counsel
  854. M/S C1SW19
  855. Unisys corporation
  856. Blue Bell
  857. Pennsylvania, 19424 (USA)
  858.  
  859. If you're occidental, you are surely using an LZW encoding every time you are
  860. speaking, especially when you use a dictionary. Let's consider, for example,
  861. the word "Cirrus". As you read a dictionary, you begin with "A", "Aa", and so
  862. on. But a computer has no experience and it must suppose that some words
  863. already exist. That is why with "Cirrus", it supposes that "C", "Ci", "Cir",
  864. "Cirr", "Cirru", and "Cirrus" exist. Of course, being that this is a computer,
  865. all these words are encoded as index numbers. Every time you go forward, you add
  866. a new number associated to the new word. Being that a computer is byte-based
  867. and not alphabetic-based, you have an initial dictionary of 256 letters instead
  868. of our 26 ('A' to 'Z') letters.
  869.  
  870. Example: Let's code "XYXYZ". First step, "X" is recognized in the initial
  871. dictionary of 256 letters as the 89th. Second step, "Y" is read. Does "XY"
  872. exist? No, then "XY" is stored as the word 256. You write in the output stream
  873. the ASCII of "X", i.e. 88. Now "YX" is tested as not referenced in the current
  874. dictionary. It is stored as the word 257. You write now in the output stream 89
  875. (ASCII of "Y"). "XY" is now met. But now "XY" is known as the reference 256.
  876. Being that "XY" exists, you test the sequence with one more letter, i.e. "XYZ".
  877. This last word is not referenced in the current dictionary. You write then the
  878. value 256. Finally, you reach the last letter ("Z"). You add "YZ" as the
  879. reference 258 but it is the last letter. That is why you just write the value
  880. 90 (ASCII of "Z").
  881.  
  882. Another encoding sample with the string "ABADABCCCABCEABCECCA".
  883.  
  884. +----+-----+---------------+------+----------+-------------------------+------+
  885. |Step|Input|Dictionary test|Prefix|New symbol|Dictionary               |Output|
  886. |    |     |               |      |          |D0=ASCII with 256 letters|      |
  887. +----+-----+---------------+------+----------+-------------------------+------+
  888. |  1 | "A" |"A" in D0      | "A"  |    "B"   | D1=D0                   |  65  |
  889. |    | "B" |"AB" not in D0 |      |          | and "AB"=256            |      |
  890. +----+-----+---------------+------+----------+-------------------------+------+
  891. |  2 | "A" |"B" in D1      | "B"  |    "A"   | D2=D1                   |  66  |
  892. |    |     |"BA" not in D1 |      |          | and "BA"=257            |      |
  893. +----+-----+---------------+------+----------+-------------------------+------+
  894. |  3 | "D" |"A" in D2      | "A"  |    "D"   | D3=D2                   |  65  |
  895. |    |     |"AD" not in D2 |      |          | and "AD"=258            |      |
  896. +----+-----+---------------+------+----------+-------------------------+------+
  897. |  4 | "A" |"D" in D3      | "D"  |    "A"   | D4=D3                   |  68  |
  898. |    |     |"DA" not in D3 |      |          | and "DA"=259            |      |
  899. +----+-----+---------------+------+----------+-------------------------+------+
  900. |  5 | "B" |"A" in D4      | "AB" |    "C"   | D5=D4                   |  256 |
  901. |    | "C" |"AB" in D4     |      |          | and "ABC"=260           |      |
  902. |    |     |"ABC" not in D4|      |          |                         |      |
  903. +----+-----+---------------+------+----------+-------------------------+------+
  904. |  6 | "C" |"C" in D5      | "C"  |    "C"   | D6=D5                   |  67  |
  905. |    |     |"CC" not in D5 |      |          | and "CC"=261            |      |
  906. +----+-----+---------------+------+----------+-------------------------+------+
  907. |  7 | "C" |"C" in D6      | "CC" |    "A"   | D7=D6                   |  261 |
  908. |    | "A" |"CC" in D6     |      |          | and "CCA"=262           |      |
  909. |    |     |"CCA" not in D6|      |          |                         |      |
  910. +----+-----+---------------+------+----------+-------------------------+------+
  911. |  8 | "B" |"A" in D7      | "ABC"|    "E"   | D8=D7                   |  260 |
  912. |    | "C" |"AB" in D7     |      |          | and "ABCE"=263          |      |
  913. |    | "E" |"ABC" in D7    |      |          |                         |      |
  914. |    |    <"ABCE" not in D7|      |          |                         |      |
  915. +----+-----+---------------+------+----------+-------------------------+------+
  916. |  9 | "A" |"E" in D8      | "E"  |    "A"   | D9=D8                   |  69  |
  917. |    |     |"EA" not in D8 |      |          | and "EA"=264            |      |
  918. +----+-----+---------------+------+----------+-------------------------+------+
  919. | 10 | "B" |"A" in D9      |"ABCE"|    "C"   | D10=D9                  |  263 |
  920. |    | "C" |"AB" in D9     |      |          | and "ABCEC"=265         |      |
  921. |    | "E" |"ABC" in D9    |      |          |                         |      |
  922. |    | "C" |"ABCE" in D9   |      |          |                         |      |
  923. |    |    <"ABCEC" not in D9>     |          |                         |      |
  924. +----+-----+---------------+------+----------+-------------------------+------+
  925. | 11 | "C" |"C" in D10     | "CCA"|          |                         |  262 |
  926. |    | "A" |"CC" in D10    |      |          |                         |      |
  927. |    |    <"CCA" not in D10|      |          |                         |      |
  928. +----+-----+---------------+------+----------+-------------------------+------+
  929.  
  930. You will notice a problem with the above output: How to write a code of 256
  931. (for example) on 8 bits? It's simple to solve this problem. You just say that
  932. the encoding starts with 9 bits and as you reach the 512th word, you use a
  933. 10-bits encoding. With 1024 words, you use 11 bits; with 2048 words, 12 bits;
  934. and so on with all numbers of 2^n (n is positive). To better synchronize
  935. the coder and the decoder with all that, most of implementations use two
  936. additional references. The word 256 is a code of reinitialisation (the codec
  937. must reinitialize completely the current dictionary to its 256 initial letters)
  938. and the word 257 is a code of end of information (no more data to read).
  939. Of course, you start your first new word as the code number 258.
  940.  
  941. You can also do so as in the GIF file format and start with an initial
  942. dictionary of 18 words to code an input stream with only letters coded on 4 bits
  943. (you start with codes of 5 bits in the output stream!). The 18 initial words
  944. are: 0 to 15 (initial letters), 16 (reinit the dictionary), and 17 (end of
  945. information). First new word has code 18, second word, code 19, ...
  946.  
  947. Important: You can consider that your dictionary is limited to 4096 different
  948. words (as in GIF and TIFF file formats). But if your dictionary is full, you
  949. can decide to send old codes *without* reinitializing the dictionary. All the
  950. decoders must be compliant with this. This enables you to consider that it is
  951. not efficient to reinitialize the full dictionary. Instead of this, you don't
  952. change the dictionary and you send/receive (depending if it's a coder or a
  953. decoder) existing codes in the full dictionary.
  954.  
  955. My codecs are able to deal as well with most of initial size of data in the
  956. initial dictionary as with full dictionary.
  957.  
  958. Let's see how to decode an LZW encoding. We saw with true dynamical Huffman
  959. scheme that you needed an header in the encoding codes. Any header is useless
  960. in LZW scheme. When two successive bytes are read, the first must exist in the
  961. dictionary. This code can be immediately decoded and written in the output
  962. stream. If the second code is equal or less than the word number in the current
  963. dictionary, this code is decoded as the first one. At the opposite, if the
  964. second code is equal to the word number in dictionary plus one, this means you
  965. have to write a word composed with the word (the sentence, not the code number)
  966. of the last code plus the first character of the last code. In between, you make
  967. appear a new word. This new word is the one you just sent to the output stream,
  968. it means composed by all the letters of the word associated to the first code
  969. and the first letter of the word of the second code. You continue the processing
  970. with the second and third codes read in the input stream (of codes)...
  971.  
  972. Example: Let's decode the previous encoding given a bit more above.
  973.  
  974. +------+-------+----------------+----------+------------------+--------+
  975. | Step | Input | Code to decode | New code |    Dictionary    | Output |
  976. +------+-------+----------------+----------+------------------+--------+
  977. |   1  |   65  |       65       |    66    |     65,66=256    |   "A"  |
  978. |      |   66  |                |          |                  |        |
  979. +------+-------+----------------+----------+------------------+--------+
  980. |   2  |   65  |       66       |    65    |     66,65=257    |   "B"  |
  981. +------+-------+----------------+----------+------------------+--------+
  982. |   3  |   68  |       65       |    68    |     65,68=258    |   "A"  |
  983. +------+-------+----------------+----------+------------------+--------+
  984. |   4  |  256  |       68       |    256   |     68,65=259    |   "D"  |
  985. +------+-------+----------------+----------+------------------+--------+
  986. |   5  |   67  |       256      |    67    |   65,66,67=260   |   "AB" |
  987. +------+-------+----------------+----------+------------------+--------+
  988. |   6  |  261  |       67       |    261   |     67,67=261    |   "C"  |
  989. +------+-------+----------------+----------+------------------+--------+
  990. |   7  |  260  |       261      |    260   |   67,67,65=262   |   "CC" |
  991. +------+-------+----------------+----------+------------------+--------+
  992. |   8  |   69  |       260      |    69    |  65,66,67,69=263 |  "ABC" |
  993. +------+-------+----------------+----------+------------------+--------+
  994. |   9  |  263  |       69       |    263   |     69,65=264    |   "E"  |
  995. +------+-------+----------------+----------+------------------+--------+
  996. |  10  |  262  |       263      |    262   |65,66,67,69,67=256| "ABCE" |
  997. +------+-------+----------------+----------+------------------+--------+
  998. |  11  |       |       262      |          |                  |  "CCA" |
  999. +------+-------+----------------+----------+------------------+--------+
  1000.  
  1001. Summary: The step 4 is an explicit example. The code to decode is 68 ("D" in
  1002. ASCII) and the new code is 256. The new word to add to the dictionary is the
  1003. letters of the first word plus the the first letter of the second code (code
  1004. 256), i.e. 65 ("A" in ASCII) plus 68 ("D"). So the new word has the letters 68
  1005. and 65 ("AD").
  1006.  
  1007. The step 6 is quite special. The first code to decode is referenced but the
  1008. second new code is not referenced being that the dictionary is limited to 260
  1009. referenced words. We have to make it as the second previously given case, it
  1010. means you must take the word to decode plus its first letter, i.e. "C"+"C"="CC".
  1011. Be care, if any encountered code is *upper* than the dictionary size plus 1, it
  1012. means you have a problem in your data and/or your codecs are...bad!
  1013.  
  1014. Tricks to improve LZW encoding (but it becomes a non-standard encoding):
  1015. - To limit the dictionary to an high amount of words (4096 words maximum enable
  1016. you to encode a stream of a maximmum 7,370,880 letters with the same dictionary)
  1017. - To use a dictionary of less than 258 if possible (example, with 16 color
  1018. pictures, you start with a dictionary of 18 words)
  1019. - To not reinitialize a dictionary when it is full
  1020. - To reinitialize a dictionary with the most frequent of the previous dictionary
  1021. - To use the codes from (current dictionary size+1) to (maximum dictionary size)
  1022. because these codes are not used in the standard LZW scheme.
  1023. Such a compression scheme has been used (successfully) by Robin Watts
  1024. <ct93008@ox.ac.uk>.
  1025.  
  1026. +==========================================================+
  1027. |                         Summary                          |
  1028. +==========================================================+
  1029.  
  1030. -------------------------------------------------
  1031. RLE type 1:
  1032. Fastest compression. Good ratio for general purpose.
  1033. Doesn't need to read the data by twice.
  1034. Decoding fast.
  1035. -------------------------------------------------
  1036. RLE type 2:
  1037. Fast compression. Very good ratio in general (even for general purposes).
  1038. Need to read the data by twice.
  1039. Decoding fast.
  1040. -------------------------------------------------
  1041. RLE type 3:
  1042. Slowest compression. Good ratio on image file,quite middle for general purposes.
  1043. Need to read the data by twice.
  1044. Change line:
  1045. #define MAX_RASTER_SIZE 256
  1046. into:
  1047. #define MAX_RASTER_SIZE 16
  1048. to speed up the encoding (but the result decreases in ratio). If you compress
  1049. with memory buffers, do not modify this line...
  1050. Decoding fast.
  1051. -------------------------------------------------
  1052. RLE type 4:
  1053. Slow compression. Good ratio on image file, middle in general purposes.
  1054. Change line:
  1055. #define MAX_RASTER_SIZE 66
  1056. into:
  1057. #define MAX_RASTER_SIZE 16
  1058. to speed up the encoding (but the result decreases in ratio). If you compress
  1059. with memory buffers, do not modify this line...
  1060. Decoding fast.
  1061. -------------------------------------------------
  1062. Huffman:
  1063. Fast compression. Good ratio on text files and similar, middle for general
  1064. purposes. Interesting method to use to compress a buffer already compressed by
  1065. RLE types 1 or 2 methods...
  1066. Decoding fast.
  1067. -------------------------------------------------
  1068. LZW:
  1069. Quite fast compression. Good, see even very good ratio, for general purposes.
  1070. Bigger the data are, better the compression ratio is.
  1071. Decoding quite fast.
  1072. -------------------------------------------------
  1073.  
  1074. The source codes work on all kinds of computers with a C compiler.
  1075. With the compiler, optimize the speed run option instead of space option.
  1076. With UNIX system, it's better to compile them with option -O.
  1077. If you don't use a GNU compiler, the source file MUST NOT have a size
  1078. over 4 Gb for RLE 2, 3, and Huffman, because I count the number
  1079. of occurrences of the bytes.
  1080. So, with GNU compilers, 'unsigned lont int' is 8 bytes instead of 4 bytes
  1081. (as normal C UNIX compilers and PCs' compilers, such as Microsoft C++
  1082. and Borland C++).
  1083. Actually:
  1084. * Normal UNIX compilers,                => 4 Gb (unsigned long int = 4 bytes)
  1085.   Microsoft C++ and Borland C++ for PCs
  1086. * GNU UNIX compilers                    => 17179869184 Gb (unsigned long int = 8 bytes)
  1087.  
  1088. +==========================================================+
  1089. |                             END                          |
  1090. +==========================================================+
  1091.